package com.heisnenhub.common.module.handler;

import com.heisnenhub.common.module.exception.RRException;
import com.heisnenhub.common.module.utils.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.net.SocketTimeoutException;

import java.util.List;
import java.util.stream.Collectors;


@Slf4j
public class BaseExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    public Api handleException(Exception e) {
        log.info("系统内部异常", e);
        return Api.fail("系统内部异常");
    }

    @ExceptionHandler(value = RuntimeException.class)
    public Api handleException(RuntimeException e) {
        log.info("系统运行异常", e);
        return Api.fail("系统运行异常");
    }

    @ExceptionHandler(value = NullPointerException.class)
    public Api handleNullException(NullPointerException e) {
        log.info("空指针异常", e);
        return Api.fail("空指针异常");
    }
    @ExceptionHandler(value = RRException.class)
    public Api handleRRException(RRException e) {
        log.info("系统错误", e);
        return Api.fail(e.getMessage());
    }




    @ExceptionHandler(value = SocketTimeoutException.class)
    public Api handleAccessDeniedException(SocketTimeoutException e){
        log.info("网络超时", e);
        return Api.fail("网络超时");
    }




    /**
     * 统一处理请求参数校验(实体对象传参)
     *
     * @param e BindException
     * @return Api
     */
    @ExceptionHandler(BindException.class)
    public Api handleBindException(BindException e) {
        StringBuilder message = new StringBuilder();
        List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
        for (FieldError error : fieldErrors) {
            message.append(error.getField()).append(error.getDefaultMessage()).append(",");
        }
        message = new StringBuilder(message.substring(0, message.length() - 1));
        return Api.fail(message.toString());
    }

    //处理请求参数格式错误 @RequestParam上validate失败后抛出的异常是javax.validation.ConstraintViolationException
    @ExceptionHandler(ConstraintViolationException.class)
    public Api ConstraintViolationExceptionHandler(ConstraintViolationException e) {
        log.info("【ConstraintViolationExceptionHandler 异常】");
        String message = e.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(Collectors.joining(","));
        return  Api.fail(message);
    }


    //处理请求参数格式错误 @RequestBody上validate失败后抛出的异常是MethodArgumentNotValidException异常。
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Api MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        log.info("【MethodArgumentNotValidException 异常】");
        String message = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining(","));
        return  Api.fail(message);
    }

}
